//
//  NRWCarCategotyIconView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/22.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWCarCategotyView.h"

////////////////////////////////////////////////优选车  准新车  练手车////////////////////////////////////////////////////
@interface NRWCarCategotyItem1View: UIView

+ (instancetype)itemViewWithFisrtTitle:(NSString *)firstTitle secondTitle:(NSString *)secondTitle imageName:(NSString *)imageName;

@property (strong, nonatomic) UIImageView *backImageView;

@property (strong, nonatomic) UILabel *firstLabel;

@property (strong, nonatomic) UILabel *secondLabel;

@end

@implementation NRWCarCategotyItem1View

+ (instancetype)itemViewWithFisrtTitle:(NSString *)firstTitle secondTitle:(NSString *)secondTitle imageName:(NSString *)imageName {
    
    NRWCarCategotyItem1View *item = [[NRWCarCategotyItem1View alloc] init];
    item.firstLabel.text = firstTitle;
    item.secondLabel.text = secondTitle;
    item.backImageView.image = getImage(imageName);
    return item;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.backImageView = [[UIImageView alloc] init];
    self.backImageView.backgroundColor = [UIColor whiteColor];
    self.backImageView.opaque = YES;
    
    self.firstLabel = DPLabel()
    .setFont(getThemeFont(16))
    .setTextColor([UIColor whiteColor])
    .setTextAlignment(NSTextAlignmentCenter);
    
    self.secondLabel = DPLabel()
    .setFont(getThemeFont(11))
    .setTextColor([UIColor whiteColor])
    .setTextAlignment(NSTextAlignmentCenter);
    
    self.addSubview(self.backImageView);
    self.backImageView.addSubview(self.firstLabel).addSubview(self.secondLabel);
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.backImageView.frame = self.bounds;
    [self.firstLabel sizeToFit];
    self.firstLabel.frame = CGRectMake(0, 5, self.bounds.size.width, self.bounds.size.height / 2);
    self.secondLabel.frame = CGRectMake(0, self.bounds.size.height / 2, self.bounds.size.width, self.bounds.size.height / 2);
}

@end

///////////////////////////////////////////////////////SUV 轿车  跑车//////////////////////////////////////////////////////
@interface NRWCarCategotyItem2View : UIView

@property (strong, nonatomic) UIImageView *topImageView;

@property (strong, nonatomic) UILabel *botLabel;

+ (instancetype)itemViewWithImageName:(NSString *)imageName title:(NSString *)title;

@end
@implementation NRWCarCategotyItem2View

+ (instancetype)itemViewWithImageName:(NSString *)imageName title:(NSString *)title {
    
    NRWCarCategotyItem2View *item = [[NRWCarCategotyItem2View alloc] init];
    item.topImageView.image = getImage(imageName);
    item.botLabel.text = title;
    return item;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.topImageView = [[UIImageView alloc] init];
        self.topImageView.backgroundColor = [UIColor whiteColor];
        self.topImageView.opaque = YES;
        
        self.botLabel = DPLabel()
        .setFont(getThemeFont(13))
        .setTextColor(getColor(@"#FF6C00"))
        .setTextAlignment(NSTextAlignmentCenter);
        
        self.addSubview(_topImageView).addSubview(_botLabel);
        
        [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top);
            make.centerX.equalTo(self.mas_centerX);
        }];
        
        [self.botLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topImageView.mas_bottom).offset(13);
            make.centerX.equalTo(self.mas_centerX);
        }];
    }
    return self;
}

@end

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@interface NRWCarCategotyView()

@end

@implementation NRWCarCategotyView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    NRWCarCategotyItem1View *item1 = [NRWCarCategotyItem1View itemViewWithFisrtTitle:@"优选车" secondTitle:@"启辕认证车源" imageName:@"home_car01"];
    NRWCarCategotyItem1View *item2 = [NRWCarCategotyItem1View itemViewWithFisrtTitle:@"准新车" secondTitle:@"1年3万公里" imageName:@"home_car02"];
    NRWCarCategotyItem1View *item3 = [NRWCarCategotyItem1View itemViewWithFisrtTitle:@"练手车" secondTitle:@"5万元以内" imageName:@"home_car03"];
    
    self.addSubview(item1).addSubview(item2).addSubview(item3);
    
    NRWCarCategotyItem2View *item4 = [NRWCarCategotyItem2View itemViewWithImageName:@"home_car_suv" title:@"精选SUV"];
    NRWCarCategotyItem2View *item5 = [NRWCarCategotyItem2View itemViewWithImageName:@"home_car_jiaoche" title:@"时尚轿车"];
    NRWCarCategotyItem2View *item6 = [NRWCarCategotyItem2View itemViewWithImageName:@"home_car_paoche" title:@"豪华跑车"];
    self.addSubview(item4).addSubview(item5).addSubview(item6);
    
    [@[item1, item2, item3] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:getAdaptationWidth(104) leadSpacing:getAdaptationWidth(16) tailSpacing:getAdaptationWidth(16)];
    
    [@[item1, item2, item3] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.height.equalTo(@55);
    }];
    
    [@[item4, item5, item6] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:getAdaptationWidth(104) leadSpacing:getAdaptationWidth(16) tailSpacing:getAdaptationWidth(16)];
    [@[item4, item5, item6] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(item1.mas_bottom).offset(22);
        make.height.equalTo(@50);
    }];
    
    NSInteger tag = 10;
    for (UIView *view in @[item1, item2, item3, item4, item5, item6]) {
        [self addTapAction:view];
        view.tag = tag;
        tag++;
    }
}

- (void)addTapAction:(UIView *)view {
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDidClick:)];
    [view addGestureRecognizer:tap];
}

- (void)tapDidClick:(UITapGestureRecognizer *)tap {
    
    CarCategotyViewClick tag = 0;
    
    switch (tap.view.tag) {
        case 10:
            tag = CarCategotyViewClickPreferred;
            break;
        case 11:
            tag = CarCategotyViewClickNewCar;
            break;
        case 12:
            tag = CarCategotyViewClickExercise;
            break;
        case 13:
            tag = CarCategotyViewClickSUV;
            break;
        case 14:
            tag = CarCategotyViewClickCar;
            break;
        case 15:
            tag = CarCategotyViewClickSportsCar;
            break;
        default:
            break;
    }
    
    if ([self.delegate respondsToSelector:@selector(carCategotyView:itemDidClickWithTag:)]) {
        
        [self.delegate carCategotyView:self itemDidClickWithTag:tag];
    }
}

@end
